﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Member.Models;
using VideoCache.Server.MVCWeb.Attributes;

namespace VideoCache.Server.MVCWeb.Areas.Member.Controllers
{
    public class NetbarController : Controller
    {
        [ActionAuthorize(AuthorizeType.Member)]
        public ActionResult Index()
        {
            if (RoleBLL.IsUserInRole(HttpContext.User.Identity.Name, "netbar"))
            {
                NetbarInfo info = NetbarBLL.GetNetbarByUserName(HttpContext.User.Identity.Name);

                NetbarModel model = new NetbarModel();

                return View(model);
            }
            else
            {
                ModelState.AddModelError("Error", "对不起，您的账号无法创建网吧。");
            }

            return View();
        }

        [ActionAuthorize(AuthorizeType.Member)]
        [HttpPost]
        public ActionResult Index(NetbarModel model)
        {
            if (ModelState.IsValid)
            {
                if (RoleBLL.IsUserInRole(HttpContext.User.Identity.Name, "netbar"))
                {
                    NetbarInfo info = new NetbarInfo();
                    info.Id = model.Id;
                    info.Name = model.Name;
                    info.Password = model.Password;
                }
            }
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(NetbarRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    UserInfo info = new UserInfo();
                    info.UserName = model.Email;
                    info.Email = model.Email;
                    info.Password = model.Password;
                    info.RegisterTime = DateTime.Now;
                    info.Id = UserBLL.CreateUser(info, new string[] { "netbar" });

                    return RedirectToAction("Login", "User");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            return View(model);
        }
    }
}